package com.ruoyi.product.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.product.domain.ProductSpec;
import com.ruoyi.product.domain.ProductSpecTemplate;
import com.ruoyi.product.domain.request.SpecDto;
import com.ruoyi.product.domain.request.TemplateCreateDto;
import com.ruoyi.product.mapper.ProductSpecMapper;
import com.ruoyi.product.mapper.ProductSpecTemplateMapper;
import com.ruoyi.product.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class TemplateServiceImpl implements TemplateService {
    @Autowired
    private ProductSpecTemplateMapper templateMapper;
    @Autowired
    private ProductSpecMapper specMapper;

    private static final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    @Transactional
    public Long createTemplate(TemplateCreateDto templateCreateDto) {
        // 创建模板
        ProductSpecTemplate template = new ProductSpecTemplate();
        template.setName(templateCreateDto.getName());
        template.setDescription(templateCreateDto.getDescription());
        template.setStatus(1L); // 默认启用
        template.setSort(0L); // 默认排序
        template.setCreatedAt(new Date());
        template.setUpdatedAt(new Date());

        templateMapper.insertTemplate(template);

        // 创建规格属性
        if (templateCreateDto.getSpecs() != null) {
            for (SpecDto specDTO : templateCreateDto.getSpecs()) {
                ProductSpec spec = new ProductSpec();
                spec.setName(specDTO.getName());
                spec.setTemplateId(template.getId());
                spec.setType(specDTO.getType());

                // 处理 valuesJson 字段
                if (specDTO.getValuesJson() != null) {
                    if (specDTO.getValuesJson() instanceof String) {
                        // 如果已经是字符串格式，直接使用
                        spec.setValuesJson((String) specDTO.getValuesJson());
                    } else  {
                        // 如果是其他格式，转换为 JSON 字符串
                        try {
                            spec.setValuesJson(objectMapper.writeValueAsString(specDTO.getValuesJson()));
                        } catch (JsonProcessingException e) {
                            spec.setValuesJson("[]");
                        }
                    }
                } else {
                    spec.setValuesJson("[]");
                }

                spec.setIsSku(specDTO.getIsSku());
                spec.setIsCustom(specDTO.getIsCustom());
                spec.setUnit(specDTO.getUnit());
                spec.setSort(specDTO.getSort());
//                spec.setCreateBy("admin");
//                spec.setUpdateBy("admin");
                spec.setCreateTime(new Date());
//                spec.setUpdateTime(new Date());
                specMapper.insertProductSpec(spec);
            }
        }

        return template.getId();
    }

    @Override
    @Transactional
    public Long updateTemplate(TemplateCreateDto templateCreateDto) {
        // 更新模板信息
        ProductSpecTemplate template = new ProductSpecTemplate();
        template.setId(templateCreateDto.getId());
        template.setName(templateCreateDto.getName());
        template.setDescription(templateCreateDto.getDescription());
        template.setUpdatedAt(new Date());

        templateMapper.updateProductSpecTemplate(template);

        // 删除原有的规格属性
        specMapper.deleteProductSpecById(template.getId());

        // 重新创建规格属性
        if (templateCreateDto.getSpecs() != null) {
            for (SpecDto specDTO : templateCreateDto.getSpecs()) {
                ProductSpec spec = new ProductSpec();
                spec.setName(specDTO.getName());
                spec.setTemplateId(template.getId());
                spec.setType(specDTO.getType());

                // 处理 valuesJson 字段
                if (specDTO.getValuesJson() != null) {
                    if (specDTO.getValuesJson() instanceof String) {
                        // 如果已经是字符串格式，直接使用
                        String valuesJson = (String) specDTO.getValuesJson();
                        // 如果是空字符串，则设置为有效的JSON空数组
                        if (valuesJson.trim().isEmpty()) {
                            spec.setValuesJson("[]");
                        } else {
                            spec.setValuesJson(valuesJson);
                        }
                    } else {
                        // 如果是其他格式，转换为 JSON 字符串
                        try {
                            spec.setValuesJson(objectMapper.writeValueAsString(specDTO.getValuesJson()));
                        } catch (JsonProcessingException e) {
                            spec.setValuesJson("[]");
                        }
                    }
                } else {
                    spec.setValuesJson("[]");
                }

                spec.setIsSku(specDTO.getIsSku());
                spec.setIsCustom(specDTO.getIsCustom());
                spec.setUnit(specDTO.getUnit());
                spec.setSort(specDTO.getSort());
                spec.setCreateTime(new Date());
                spec.setUpdateTime(new Date());
                specMapper.insertProductSpec(spec);
            }
        }

        return template.getId();
    }
}
